﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL;
using BLL.Managers;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for PasswordRecoveryScreen.xaml
    /// </summary>
    public partial class PasswordRecoveryWindow : Window
    {
        public PasswordRecoveryWindow()
        {
            InitializeComponent();
        }

        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            string text = userNameTextBox.Text;
            GymWorker gymWorker;
            gymWorker = GymWorkerManager.Instance.GetWorkerByUserName(text);
            if (gymWorker == null)
            {
                gymWorker = GymWorkerManager.Instance.GetWorkerByMailAdress(text);
                if (gymWorker == null)
                {
                    resultLabel.Content = "Incorrect username of email adress";
                }
                else
                {
                    sendRecoveryMail(gymWorker);
                    this.Close();
                }

            }
            else
            {
                sendRecoveryMail(gymWorker);
                this.Close();
            }

        }

        private void sendRecoveryMail(GymWorker gymWorker)
        {
            string subject = "Your account information";
            string content = string.Format("{0}, your account details: \nUser name: {1} \nPassword: {2}", gymWorker.FirstName, gymWorker.UserName, gymWorker.Password);

            EmailService.SendEmail(subject, content, gymWorker.Email);
        }
    }
}
